
package lab.four.customviews;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class Lab4CustomViewsActivity extends Activity 
{
	private enum AudioState
	{
		Playing,
		Paused,
		Preparing,
		Finished
	}
	
	// Fields.
	private PlayButton m_playButton;
	private AudioState m_audioState;
	private MediaPlayer m_mp;
	private static final String MEDIA_PATH = "" + Environment.getExternalStorageDirectory();
	private static final String SONG_NAME = "/venomsoundtrack.mp3";
	
	private OnClickListener m_onClickListener = new OnClickListener() 
	{
		public void onClick(View v) 
		{
			if (m_audioState == AudioState.Playing)
			{
				((PlayButton) v).setTextColor(Color.GREEN);
				((PlayButton) v).setText("Play");
				m_mp.pause();
				m_audioState = AudioState.Paused;
			}
			else if (m_audioState == AudioState.Paused || m_audioState == AudioState.Finished)
			{
				((PlayButton) v).setTextColor(Color.RED);
				((PlayButton) v).setText("Pause");
				PlaySong();
				m_audioState = AudioState.Playing;
			}
			else if (m_audioState == AudioState.Preparing)
			{
				((PlayButton) v).setTextColor(Color.RED);
				((PlayButton) v).setText("Pause");
				PlaySong();
				m_audioState = AudioState.Playing;
			}
			
			Log.d("debug-event", "State: " + m_audioState);
		}
	};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Log.d("debug-event", "Path: " + MEDIA_PATH);
        
        m_playButton = new PlayButton(this, "Play", Color.GREEN);
        setContentView(m_playButton);
        m_playButton.setOnClickListener(m_onClickListener);
        
        ActionBar actionBar = getActionBar();
        actionBar.hide();
        
        m_audioState = AudioState.Preparing;
        
        m_mp = new MediaPlayer();
    }
    
    private void PlaySong()
    {
    	if (m_audioState == AudioState.Preparing)
    	{
    		Log.d("debug-event", "Full path: " + MEDIA_PATH + SONG_NAME);
    		m_mp.reset();
    		try 
    		{
				m_mp.setDataSource(MEDIA_PATH + SONG_NAME);
				m_mp.prepare();
				m_mp.start();
				Log.d("debug-event", "Song should now be played.");
				
				m_mp.setOnCompletionListener(new OnCompletionListener() 
				{
					public void onCompletion(MediaPlayer a_mp) 
					{
						m_audioState = AudioState.Finished;
						m_playButton.setTextColor(Color.GREEN);
						m_playButton.setText("Play");
						
					}
				});
			} 
    		catch (Exception e) 
			{
    			e.printStackTrace();
    			Log.d("debug-event", "Error: " + e.toString());
			}
    	}
    	else
    	{
    		if (m_mp.isPlaying() == false)
    		{
    			m_mp.start();
    		}
    	}
    }

    
    
    private class PlayButton extends View
    {
    	// Fields.
    	private Paint m_paint;
    	private String m_text;

		public PlayButton(Context a_context, String a_defaultText, int a_color) 
		{
			super(a_context);
			
			m_text = a_defaultText;
			
			m_paint = new Paint();
			m_paint.setColor(a_color);
			m_paint.setTextSize(0.25f);
			m_paint.setAntiAlias(true);
		}
		
		@Override
		protected void onDraw(Canvas a_canvas) 
		{
			float scaleX = (float)getWidth();
			float scaleY = (float)getHeight();
			
			a_canvas.save(Canvas.MATRIX_SAVE_FLAG);
			a_canvas.scale(scaleX, scaleY);
			
			Paint backgroundPaint = new Paint();
			backgroundPaint.setColor(Color.GRAY);
			
			if (m_audioState == AudioState.Playing)
			{
				// Pause
				a_canvas.drawRect(0.0f, 0.0f, 1.0f, 1.0f, backgroundPaint);
				a_canvas.drawText(m_text, 0.125f, 0.6f, m_paint);				
			}
			else
			{
				// Play
				a_canvas.drawRect(0.0f, 0.0f, 0.8f, 1.0f, backgroundPaint);
				a_canvas.drawText(m_text, 0.125f, 0.6f, m_paint);
			}
			
			
		}
		
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
		{
			setMeasuredDimension(350, 100);
		}
		
		
		private void setText(String a_text)
		{
			m_text = a_text;
			requestLayout();
			// Do I need invalidate aswell?
		}
		
		private void setTextColor(int a_color)
		{
			m_paint.setColor(a_color);
			invalidate();
		}
    	
		@Override
		public boolean onTouchEvent(MotionEvent a_event) 
		{
			invalidate();
			return super.onTouchEvent(a_event);
		}
		
    }
}